package cn.lyf.myblog.controller.api;

import java.util.concurrent.*;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class EmailUtils {
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
    Map<String, ele> map = new ConcurrentHashMap<>();
    @Autowired
    JavaMailSenderImpl mailSender;

    String name="MyBlog";
    String from="MyBlog验证中心";

    class ele {
        String code;
        Date date;

        public ele(String code, Date date) {
            this.code = code;
            this.date = date;
        }
    }

    public void sendEmail(String email) throws MailException,MessagingException {
        // 验证码
        int code = (int) ((Math.random() * 9 + 1) * 1000);
        String verCode = String.valueOf(code);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date();
        String time = simpleDateFormat.format(date);
        map.put(email, new ele(verCode, date));
        // 创建计时线程池，到时间自动移除验证码
        scheduledExecutorService.schedule(new Thread(() -> {
            ele e = map.get(email);
            if (e != null && e.date.equals(date)) {
                map.remove(email);
            }
        }), 5 * 60, TimeUnit.SECONDS);
        // 一下为发送邮件部分
        MimeMessage mimeMessage = null;
        MimeMessageHelper helper = null;
        // 发送复杂的邮件
        mimeMessage = mailSender.createMimeMessage();
        // 组装
        helper = new MimeMessageHelper(mimeMessage, true);
        // 邮件标题
        helper.setSubject("【"+name+"】 注册账号验证码");
        // 因为设置了邮件格式所以html标签有点多，后面的ture为支持识别html标签
        // 想要不一样的邮件格式，百度搜索一个html编译器，自我定制。
        helper.setText("<h3>\n" + "\t<span style=\"font-size:16px;\">亲爱的用户：</span> \n" + "</h3>\n" + "<p>\n"
                + "\t<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style=\"font-size:14px;\">&nbsp; <span style=\"font-size:16px;\">&nbsp;&nbsp;您好！您正在进行邮箱验证，本次请求的验证码为：<span style=\"font-size:24px;color:#FFE500;\"> "
                + verCode
                + "</span>,本验证码5分钟内有效，请在5分钟内完成验证。（请勿泄露此验证码）如非本人操作，请忽略该邮件。(这是一封自动发送的邮件，请不要直接回复）</span></span>\n"
                + "</p>\n" + "<p style=\"text-align:right;\">\n"
                + "\t<span style=\"background-color:#FFFFFF;font-size:16px;color:#000000;\"><span style=\"color:#000000;font-size:16px;background-color:#FFFFFF;\"><span class=\"token string\" style=\"font-family:&quot;font-size:16px;color:#000000;line-height:normal !important;background-color:#FFFFFF;\">"+name+"</span></span></span> \n"
                + "</p>\n" + "<p style=\"text-align:right;\">\n"
                + "\t<span style=\"background-color:#FFFFFF;font-size:14px;\"><span style=\"color:#FF9900;font-size:18px;\"><span class=\"token string\" style=\"font-family:&quot;font-size:16px;color:#000000;line-height:normal !important;\"><span style=\"font-size:16px;color:#000000;background-color:#FFFFFF;\">"
                + time
                + "</span><span style=\"font-size:18px;color:#000000;background-color:#FFFFFF;\"></span></span></span></span> \n"
                + "</p>", true);
        // 收件人
        helper.setTo(email);
        // 发送方
        helper.setFrom("3365310020@qq.com");

        // 发送邮件
        mailSender.send(mimeMessage);
        // 发送验证码成功
    }

    boolean verification(String email, String code) {
        ele e = map.get(email);
        if (e != null && e.code.equals(code)) {
            return true;
        }
        return false;
    }
}
